package 锁.lock.ProduceConsumer;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2023/9/5 15:30
 * @description:
 * @version:
 */
public class ProduceCost {
    Lock lock = new ReentrantLock();
    Condition condition = lock.newCondition();
    boolean flag;

    public void showA() {
        try {
            lock.lock();
            while (flag) {
                condition.await();
                System.out.println(Thread.currentThread().getName() + "   " + "*****");
            }
            flag = true;
            condition.signalAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }

    public void showB() {
        try {
            lock.lock();
            while (!flag) {
                condition.await();
                System.out.println(Thread.currentThread().getName() + "   " + "-----");
            }
            flag = false;
            condition.signalAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
}
